var game = new Phaser.Game(800,600,Phaser.Canvas,'',{preload:preload,create:create,update:update});

function preload() {
    game.load.image('sky', 'assets/sky.png');
    game.load.image('ground', 'assets/platform.png');
    game.load.image('star', 'assets/star.png');
    game.load.image('grass', 'assets/grass.png');
    game.load.image('ice', 'assets/ice.png');
    game.load.spritesheet('dude', 'assets/dude.png', 32, 48);

}

var player;
var platforms;
var cursors;

var stars;
var score = 0;
var scoreText;
var scoreText2;

function create() {

	game.world.setBounds(0,0,4000,900);
	game.add.tileSprite(0,0,4000,900,'sky');

    game.physics.startSystem(Phaser.Physics.ARCADE);

    platforms = game.add.group();

    platforms.enableBody = true;

    var ground = platforms.create(3200, game.world.height - 64, 'ground');

    ground.scale.setTo(2);

    ground.body.immovable = true;


    ground = platforms.create(0, game.world.height - 128, 'ground');

    ground.scale.setTo(2);

    ground.body.immovable = true;


    player = game.add.sprite(100, game.world.height - 430, 'dude');

    game.physics.arcade.enable(player);

    player.body.bounce.y = 0.1;
    player.body.gravity.y = 300;
    player.body.collideWorldBounds = true;

    game.camera.follow(player);	

    player.animations.add('left', [0, 1, 2, 3], 10, true);
    player.animations.add('right', [5, 6, 7, 8], 10, true);

    stars = game.add.group();

    stars.enableBody = true;

   
        
    for (var x = 0; x < 15; x+=1)
    {
         var star = stars.create(Math.random()*800, Math.random()*400, 'star');
        star.body.gravity.y = 600;
        star.body.bounce.y = 0.4 + Math.random() * 0.2;
    } 

     for (var x = 0; x < 25; x+=1)
    {
        var star = stars.create(3950,0, 'star');
        star.body.gravity.y = 800;
        star.body.bounce.y = 0.4 + Math.random() * 0.2;
    } 

    for (var x = 0; x < 750; x+=50)
    {
       var star = stars.create(x,850, 'star');
    } 

    for (var x = 0; x < 4000; x+=100)
    {
        var star = stars.create(x,0, 'star');
    } 

    scoreText = game.add.text(3800, 800, 'Score: 0', { fontSize: '32px', fill: '#000' });
    scoreText2 = game.add.text(0, 860, 'Score: 0', { fontSize: '32px', fill: '#000' });
    game.add.text(300, 800, '—————往这边跳，有惊喜—————>', { fontSize: '32px', fill: '#000' });

    cursors = game.input.keyboard.createCursorKeys();
    
}

function update() {

    game.physics.arcade.collide(player, platforms);
    game.physics.arcade.collide(stars, platforms);

    game.physics.arcade.overlap(player, stars, collectStar, null, this);

    player.body.velocity.x = 0;

    if (cursors.left.isDown)
    {
        player.body.velocity.x = -700;

        player.animations.play('left');
    }
    else if (cursors.right.isDown)
    {
        player.body.velocity.x = 700;

        player.animations.play('right');
    }
    else
    {
        player.animations.stop();

        player.frame = 4;
    }
    
    if (cursors.up.isDown && player.body.touching.down)
    {
        player.body.velocity.y = -500;
    }
    if (cursors.down.isDown)
    {
        player.body.velocity.y = 1000;
    }
}

function collectStar (player, star) {
    
    star.kill();
    score += 100;
    scoreText.text = 'Score: ' + score;
    scoreText2.text = 'Score: ' + score;

    if(score==5500)
    {

    player.body.bounce.y = 0;
    player.body.gravity.y = -700;

    }
    var s=0;
    if(score==9500)
    {

    player.body.bounce.y = 0.5;
    player.body.gravity.y = 100;
    
    for (var x = 0; x < 4000; x+=20)
    {
        var star = stars.create(x,700, 'star');
        star.body.gravity.y = 10000;
        star.body.bounce.y = 0;
    } 
    }
    if(score==17600)
    {

    player.body.bounce.y = 0;
    player.body.gravity.y = -3;
    
    for (var x = 0; x < 4000; x+=50)
    {
        var star = stars.create(x,600, 'star');
        star.body.gravity.y = -1;
        star.body.bounce.y = 0;
    } 
    }
    if(score==22200)
    {
    
    player.body.bounce.y = 0;
    player.body.gravity.y = 1000000;
    for (var x = 1000; x < 3000; x+=50)
    {
        var star = stars.create(x,600, 'star');
        star.body.gravity.y = 10000;
        star.body.bounce.y = 0;
        star.body.collideWorldBounds = true;
    } 
        if(score==26200)
    {

    game.add.text(2000, 750, '你赢了！！！', { fontSize: '64px', fill: '#000' });

    }

    }


}